const express = require('express')
const cheerio = require('cheerio')
const app = express()
const port = 3000

const foodServer = require('./index.js')

app.get('/',(req,res)=>{
	let describe = 
	"<h1 align='center'>基于NodeJS 的网页爬取（以菜谱为例）</h1>"+
	"1. 测试接口： /test <a href='/test' target='_bank'>点击测试</a><br/>"+
	"2. 获取每日三餐接口： /getTodayFood <a href='/getTodayFood' target='_bank'>点击测试</a><br/>" +
	"3. 获取首页推荐列表接口： /homeFoodList <a href='/homeFoodList' target='_bank'>点击测试</a><br/>" +
	"4. 查询菜谱接口： /searchFood?foodName=食物名称&pageNum=1 <a href='/searchFood?foodName=蛋炒饭&pageNum=1' target='_bank'>点击测试（以蛋炒饭为例）</a><br/>" +
	"5. 查看食物详情接口： /foodDetail?url=指定食物的href <a href='/foodDetail?url=http://www.meishij.net/zuofa/danchaofan_163.html' target='_bank'>点击测试</a><br/>"
	res.send(describe)
	
})

app.get('/test',(req,res)=>{
	foodServer.test().then(result=>{
		res.send(result);
	})
	
})

app.get('/getTodayFood',(req,res)=>{
	foodServer.getTodayFood().then(result=>{
		res.send(result);
	})
})

app.get('/homeFoodList',(req,res)=>{
	foodServer.homeFoodList().then(result=>{
		res.send(result);
	})
})

app.get('/searchFood',(req,res)=>{
	foodServer.searchFood(req.query.foodName,req.query.pageNum).then(result=>{
		res.send(result);
	})
})

app.get('/foodDetail',(req,res)=>{
	foodServer.foodDetail(req.query.url).then(result=>{
		res.send(result);
	})
})

app.listen(port,()=>{
	console.log(`node-reptile server is running: http://localhost:${port}`)
})